package com.cashmate.struts;

import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.utilities.ClassUtils;
import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

@Action(value = "/select-branches", results = {
    @Result(name = "success", location = "../fragments/branches.jsp"),
    @Result(name = "select", type = "json"),
    @Result(name = "error", location = "../error/messages.jsp")
})
public class ForGridSelectBranches extends ActionSupport {

    private static Logger logger = Logger.getLogger(ForGridSelectBranches.class);
    private final BranchDAO branch = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private Map<String, String> branches;
    private String forDccr;
    private String forPerf;
    private String branchname;

    @Override
    public String execute() throws Exception {
        try {
            System.out.println(ClassUtils.toString(this));
            if (forPerf == null) {
                branches = new HashMap<String, String>();
                DetachedCriteria criteria = DetachedCriteria.forClass(Branches.class);
                criteria.add(Restrictions.eq("isactive", true));
                criteria.addOrder(Order.asc("branchname"));
                List<Branches> bList = branch.findByCriteria(criteria);
                for (Branches b : bList) {
                    //System.out.println(b.getBranchcode());
                    if (b.getBranchid() != 1) {
                        branches.put(b.getBranchid().toString(), b.getBranchname());
                    }
                }
            } else {
                Branches b = branch.get(Integer.parseInt(forPerf));
                branchname = b.getBranchname();
            }

        } catch (Exception ex) {
            Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + ex.getLocalizedMessage(), ex);
            } else {
                logger.fatal(ex.getLocalizedMessage(), ex);
            }
            addActionError(ex.getLocalizedMessage());
            return ERROR;
        } finally {
            BranchDAO.close();
        }
        if (forDccr != null || forPerf != null) {
            return "select";
        }
        return SUCCESS;
    }

    public Map<String, String> getBranches() {
        return branches;
    }

    public void setForDccr(String forDccr) {
        this.forDccr = forDccr;
    }

    public String getBranchname() {
        return branchname;
    }

    public void setForPerf(String forPerf) {
        this.forPerf = forPerf;
    }
}
